<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!--<link rel="stylesheet" href="static/assets/css/amazeui.min.css"/>-->
    <link rel="stylesheet" href="static/syntaxhighlighter_3.0.83/styles/shCore.css">
    <link rel="stylesheet" href="static/syntaxhighlighter_3.0.83/styles/shCoreEclipse.css">
    <script src="static/assets/js/jquery.min.js"></script>
    <script src="static/layer/layer.js"></script>
    <script src="static/syntaxhighlighter_3.0.83/scripts/shCore.js"></script>
    <script src="static/syntaxhighlighter_3.0.83/scripts/shBrushJava.js"></script>
</head>
<body>
<span th:if="${note!=null}">
<input type="hidden" id="noteid" th:value="${note.id}">
<input type="hidden" id="notecontent" th:value="${note.content}">
</span>
<div th:fragment="ueditormode" class="am-cf">
    <!-- 加载编辑器的容器 -->
    <div id="ueditor" name="ueditor" type="text/plain" style="top: 20px;">这里写你的初始化内容</div>

    <!-- 配置文件 -->
    <script type="text/javascript" src="static/ueditor1_4/ueditor.config.js"></script>
    <script type="text/javascript">
        //hewei
        function getHostPath() {
            //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
            var curWwwPath = window.document.location.href;
            console.log(location.href);
            //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
            var pathName = window.document.location.pathname;
            console.log("当前路径：" + pathName);
            var pos = curWwwPath.indexOf(pathName);
            //获取主机地址，如： http://localhost:8083
            var hostPath = curWwwPath.substring(0, pos);
            //获取带"/"的项目名，如：/uimcardprj
            var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
            console.log("项目路径：" + hostPath);
            return hostPath;
            // return curWwwPath;
        }

        window.UEDITOR_CONFIG.serverUrl = getHostPath() + "/ueditor";
    </script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="static/ueditor1_4/ueditor.all.js"></script>

    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        $(function () {

            var content =$("#notecontent").val();
            // var ue = UE.getEditor('ueditor').render("ueditor");

            // 删除之前的实例，重新实例化，避免第二次无法实例化以及第一次重复渲染
            UE.delEditor('ueditor');
            var ue = UE.getEditor('ueditor');
            // console.log(window.UEDITOR_HOME_URL);
            console.log("加载初始化ueditor......");
            UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
            UE.Editor.prototype.getActionUrl = function (action) {
                if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadvideo') {
                    return getHostPath() + '/imgUpload';
                    //'http://localhost:8080/imgUpload';为方法imgUpload的访问地址
                } else {
                    return this._bkGetActionUrl.call(this, action);
                }
            }
            ue.commands['jjsave'] = {
                execCommand: function () {
                    // this.execCommand('insertHtml', "<img src='https://www.baidu.com/img/bd_logo1.png' />");
                    // alert("保存成功");
                    var title = "";
                    var bookid = "";
                    var noteid="";
                    var content = "";
                    var imgcover = "";
                    var data = {};
debugger;
                    var pageType=localStorage.getItem("pageType");
                    if (pageType=="add") {
                        title = localStorage.getItem("notetitle");
                        bookid = localStorage.getItem("bookid");
                        content = ue.getContent() + "";
                        imgcover = "";
                        data = {notebookId: bookid, title: title, content: content, imgcover: imgcover};
                        $.ajax({
                            type: 'post',
                            url: '/savenote',
                            //发送的是Json对象
                            contentType: 'application/json',
                            //接收后的数据是Json对象
                            dataType: 'json',
                            data: JSON.stringify(data),
                            // data:data,
                            success: function (result) {
                                debugger;
                                // result=JSON.parse(result);
                                console.log(result);
                                var errmsg = "";
                                if (result.success == "no") {
                                    layer.alert(result.ErrorMsg, {icon: 2});
                                } else {
                                    // window.location.href='/login';
                                    layer.msg("保存成功！");
                                }
                            },
                            error: function (result) {
                                layer.msg("发生异常，请稍后再试！", {icon: 2, time: 1000});
                                console.log(result);
                            }
                        });
                    }else if (pageType=="edit"){
                        // title = localStorage.getItem("notetitle");
                        noteid = localStorage.getItem("noteid");
                        content = ue.getContent() + "";
                        imgcover = "";
                        data = {id: noteid,content: content};
                        $.ajax({
                            type: 'post',
                            url: '/updatenote',
                            //发送的是Json对象
                            contentType: 'application/json',
                            //接收后台的数据是Json对象
                            dataType: 'json',
                            data: JSON.stringify(data),
                            // data:data,
                            success: function (result) {
                                debugger;
                                // result=JSON.parse(result);
                                console.log(result);
                                var errmsg = "";
                                if (result.success == "no") {
                                    layer.alert(result.ErrorMsg, {icon: 2});
                                } else {
                                    // window.location.href='/login';
                                    layer.msg("保存成功！");
                                }
                            },
                            error: function (result) {
                                layer.msg("发生异常，请稍后再试！", {icon: 2, time: 1000});
                                console.log(result);
                            }
                        });
                    }

                    return true;
                },
                queryCommandState: function () {
                }
            };

            UE.getEditor('ueditor').addListener("ready", function () {
                UE.getEditor('ueditor').setContent(content);
            });
            SyntaxHighlighter.highlight(); //执行代码高亮
        })
    </script>
</div>

<script src="static/assets/js/amazeui.min.js"></script>
</body>
</html>